﻿using BCL.Tool.Extend;
using BCL.WPFComponent.Base;
using DevicesView.Logic.App;
using Microsoft.Win32;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using BCL.Tool;
namespace DevicesView.Config
{
    /// <summary>
    /// UCOrgConfig.xaml 的交互逻辑
    /// </summary>
    public partial class UCOrgConfig : BaseFunctionControl
    {
        private byte[] Logo;
        private byte[] QRCode;

        public object ResetButton { get; private set; }

        public UCOrgConfig()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Entity.HospitalInfo info = AppResource.CurrentResource.Hospital;
            if (info != null)
            {
                textEditName.Text = info.Name;
                textEditAddress.Text = info.Address;
                textEditPhone.Text = info.Phone;
                textEditRemarks.Text = info.Remarks;
                if (info.Logo != null)
                    pictureEditLogo.Source = info.Logo.ToBitmap();

                if (info.QRCode != null)
                    pictureEditQRCode.Source = info.QRCode.ToBitmap();
                textEditNum.Text = info.Code;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Entity.HospitalInfo info = AppResource.CurrentResource.Hospital;
            info.Name = textEditName.Text;// = info.Name;
            info.Address = textEditAddress.Text;// = info.Address;
            info.Phone = textEditPhone.Text;
            info.Remarks = textEditRemarks.Text;// = info.Remarks;
            info.Code = textEditNum.Text;

            if (Logo != null)
                info.Logo = Logo;

            if (QRCode != null)
                info.QRCode = QRCode;

            AppResource.CurrentResource.Save();
            BCL.WPFComponent.UCMessageBox.Show("保存成功".Language());
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            UserControl_Loaded(sender, e);
        }

        private void PictureEditLogo_MouseUp(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Image Files(*.BMP;*.JPG;*.png)|*.BMP;*.JPG;*.png";
            if (of.ShowDialog() == true)
            {
                Logo = System.IO.File.ReadAllBytes(of.FileName);
                this.pictureEditLogo.Source = Logo.ToBitmap();
            }
        }

        private void PictureEditQRCode_MouseUp(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Image Files(*.BMP;*.JPG;*.png)|*.BMP;*.JPG;*.png";
            if (of.ShowDialog() == true)
            {
                QRCode = File.ReadAllBytes(of.FileName);
                this.pictureEditQRCode.Source = QRCode.ToBitmap();
            }
        }

        public override void LanguageBinding()
        {

        }
    }
}
